package top.zxybird.common.model;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;

public class SsoSession implements Serializable {
  /**
   * 
   */
  private static final long serialVersionUID = 1824908669735890261L;

  private Long uid;

  private String ticket;

  private boolean isRemember;

  public SsoSession() {

  }

  public SsoSession(Long uid, String ticket, boolean isRemember) {
    this.setUid(uid);
    this.setTicket(ticket);
    this.setIsRemember(isRemember);
  }

  public SsoSession(String uidAndToken, boolean isRemember) {
    if (StringUtils.isEmpty(uidAndToken)) {
      return;
    }
    int i = uidAndToken.indexOf("&");
    String userId = uidAndToken.substring(0, i);
    String ticket = uidAndToken.substring(i + 1);
    this.setUid(Long.valueOf(userId));
    this.setTicket(ticket);
    this.setIsRemember(isRemember);
  }

  public static SsoSession parse(String uidAndToken, boolean isRemember) {
    SsoSession ssoSession = new SsoSession();
    if (StringUtils.isEmpty(uidAndToken)) {
      return ssoSession;
    }
    int i = uidAndToken.indexOf("&");
    String userId = uidAndToken.substring(0, i);
    String ticket = uidAndToken.substring(i + 1);
    ssoSession.setUid(Long.valueOf(userId));
    ssoSession.setTicket(ticket);
    ssoSession.setIsRemember(isRemember);
    return ssoSession;
  }

  public boolean notEmpty() {
    return null != uid && null != ticket;
  }

  public Long getUid() {
    return uid;
  }

  public void setUid(Long uid) {
    this.uid = uid;
  }

  public String getTicket() {
    return ticket;
  }

  public void setTicket(String ticket) {
    this.ticket = ticket;
  }

  public boolean getIsRemember() {
    return isRemember;
  }

  public void setIsRemember(boolean isRemember) {
    this.isRemember = isRemember;
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("SsoSession:[uid=").append(uid).append(",ticket=").append(ticket).append(",isRemember=")
        .append(isRemember);
    return sb.toString();
  }

  public static void main(String[] args) {
    System.out.println(SsoSession.class.getSimpleName());
  }

}
